import { defineStore } from 'pinia';
import { useUserApi } from '@/api/user';
import { CryptoUtils, RKUtils } from '@/utils';

export const useSettingStore = defineStore('setting', {
	state: () => ({
		// 系统模式：['custom', 'develop', 'development', 'production']
		envMode: import.meta.env.MODE,
		// 加解密密钥
		secretKey: '',
		// 是否开启加密
		encryptOpen: RKUtils.parseStringToBoolean(import.meta.env.VITE_ENCRYPT_OPEN),
		// 是否开启Router的web hash 默认开启
		openWebHash: RKUtils.parseStringToBoolean(import.meta.env.VITE_OPEN_WEB_HASH),
		// 是否开启主题设置 true-开启 false-关闭
		openThemeSetting: RKUtils.parseStringToBoolean(import.meta.env.VITE_OPEN_THEME_SETTING),
		// 是否开启复制主题按钮 true-开启 false-关闭
		openThemeCopyBtn: RKUtils.parseStringToBoolean(import.meta.env.VITE_OPEN_THEME_COPY_BTN),
	}),
	actions: {
		setSecretKey() {
			return new Promise((resolve, reject) => {
				useUserApi()
					.getUserAuth()
					.then((res) => {
						const { code, data, msg } = res;
						if (code === 200) {
							const { signature } = data;
							this.secretKey = signature;
							CryptoUtils.setCryptoSecretKey(signature);
							resolve(data);
						} else {
							reject(msg);
						}
					})
					.catch((error) => {
						reject(error);
					});
			});
		},
	},
});
